package weaver.workflow.actionsupport;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class ClassScaner {
	private static final String DOT = ".";
	private static final String SUFFIX  = ".class";
	private final List<Class<?>> classes = new ArrayList<Class<?>>();
	
	public List<Class<?>> execute() throws ClassNotFoundException {
		String packageName = "weaver.interfaces.workflow";
		String path = packageName.replace(DOT, "\\");		
		String java_home = System.getProperty("java.home");
		StringBuffer sb = new StringBuffer();
		String[] args = java_home.split("\\\\");
		for(int i=0;i<args.length-2;i++){
			sb.append(args[i]).append("\\");
		}
		String classPath = sb.toString()+"ecology\\classbean\\"+path;
		fillClasses(new File(classPath),packageName, classes);
		return classes;
	}

	/**
	 * 填充满足条件的class 填充到 classes
	 * @param file    类路径下的文件
	 * @param packageName   需要扫面的包名
	 * @param classFilter   class过滤器
	 * @param classes       List 集合
	 * @throws ClassNotFoundException 
	 */
	private static void fillClasses(File file, String packageName, List<Class<?>> classes) throws ClassNotFoundException {		
		if (isDirectory(file)) {
			processDirectory(file, packageName, classes);
		}else if (isClass(file.getName())) {
			processClassFile(file, packageName, classes);
		}
	}
	
	/**
	 * 处理如果为目录的情况,需要递归调用 fillClasses方法
	 * @param directory
	 * @param packageName
	 * @param classFilter
	 * @param classes
	 * @throws ClassNotFoundException 
	 */
	private static void processDirectory(File directory, 
			String packageName, List<Class<?>> classes) throws ClassNotFoundException {
		for (File file : directory.listFiles(fileFilter)) {
			fillClasses(file, packageName, classes);
		}
	}

	/**
	 * 处理为class文件的情况,填充满足条件的class 到 classes
	 * @param file
	 * @param packageName
	 * @param classFilter
	 * @param classes
	 * @throws ClassNotFoundException 
	 */
	private static void processClassFile(File file, String packageName, List<Class<?>> classes) throws ClassNotFoundException {
		final String filePathWithDot = file.getAbsolutePath().replace(File.separator, DOT);
		int subIndex = -1;
		if ((subIndex = filePathWithDot.indexOf(packageName)) != -1) {
			final String className = filePathWithDot.substring(subIndex).replace(SUFFIX, "");
			fillClass(className, packageName, classes);
		}
	}
	
	/**
	 * 填充class 到 classes
	 * @param className
	 * @param packageName
	 * @param classes
	 * @param classFilter
	 * @throws ClassNotFoundException 
	 */
	private static void fillClass(String className, String packageName,List<Class<?>> classes) throws ClassNotFoundException {
		if(checkClassName(className, packageName)) {		
			//获取实体类
			final Class<?> clazz = Class.forName(className, Boolean.FALSE, ClassScaner.class.getClassLoader());
			//继承的父类型判断
	        if(clazz.getSuperclass() == ActionSupport.class){
				classes.add(clazz);
	        }
		}
	}
	
	/**
	 * 检查该类是否是该包名下的
	 * @param className
	 * @param packageName
	 * @return
	 */
	private static boolean checkClassName(String className, String packageName) {
		return className.indexOf(packageName) == 0;
	}
	
	/**
	 * 判断是否为class类型的文件
	 * @param fileName
	 * @return
	 */
	private static boolean isClass(String fileName) {
		return fileName.endsWith(SUFFIX);
	}

	/**
	 * 判断是否为文件夹
	 * @param file
	 * @return
	 */
	private static boolean isDirectory(File file) {
		return file.isDirectory();
	}
	
	/**文件过滤器，过滤掉不需要的文件(过滤内部类)**/
	private static FileFilter fileFilter = new FileFilter() {
		public boolean accept(File pathname) {
			return (isClass(pathname.getName()) && !pathname.getName().contains("$")) || isDirectory(pathname);
		}
	};
}
